package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.juneng.common.Condition;
import com.juneng.dto.CustomerServiceDto;
import com.juneng.entity.CustomerService;
import com.juneng.entity.OrderUser;
import com.juneng.mapper.CustomerServiceMapper;
import com.juneng.mapper.OrderUserMapper;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.User;
import com.juneng.mapper.UserMapper;
import com.juneng.service.IUserService;
import com.juneng.dto.UserDto;
import com.juneng.dto.UserQueryDto;
import com.juneng.vo.DashboardResponse;
import com.juneng.vo.OrderUserVo;
import com.juneng.vo.UserVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息 服务实现类
 *
 * @author zds
 * @since 2021-07-08
 */
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    protected UserMapper userMapper;

    protected CustomerServiceMapper customerServiceMapper;

    protected OrderUserMapper orderUserMapper;

    @Override
    public IPage<UserVo> page(UserQueryDto dto) {
        IPage<UserVo> page = Condition.getPage(dto);
        return userMapper.page(page,dto);
    }

    @Override
    public List<User> list(UserQueryDto dto) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto,User.class));
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(UserDto dto) {
        return userMapper.insert(BeanCopyUtils.copy(dto,User.class));
    }

    @Override
    public Integer updateById(UserDto dto) {
        return userMapper.updateById(BeanCopyUtils.copy(dto,User.class));
    }

    @Override
    public Integer deleteLogic(List<Integer> toIntList) {
        return userMapper.deleteBatchIds(toIntList);
    }

    @Override
    public User getById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public String customerService(CustomerServiceDto dto) {
        CustomerService customerService = customerServiceMapper.selectOne(new LambdaQueryWrapper<CustomerService>().eq(CustomerService::getCustomerServiceId, dto.getCustomerServiceId()));
        return customerService.getContent();
    }

    /**
     * 数据显示
     * @return
     */
    @Override
    public DashboardResponse getDashboard() {
        //数据看板缺少数据：今日注册用户、总注册用户、今日订单、总订单、今日交易额、总交易额
        DashboardResponse response = new DashboardResponse();
        //今日注册用户数
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("create_date",LocalDate.now());
        List<User> users3 = userMapper.selectList(wrapper);
        response.setRegisteredUserNum(users3.size());
        List<User> users2 = userMapper.selectList(new LambdaQueryWrapper<>());
        //总注册用户数
        List<User> users1 = userMapper.selectList(new LambdaQueryWrapper<>());
        response.setTotalRegistrationUserNum(users1.size());
        //今日订单数
       // QueryWrapper<OrderUser> orderUserWrapper = new QueryWrapper<>();
        List<OrderUser> orderUsers = orderUserMapper.selectList(new LambdaQueryWrapper<OrderUser>().gt(OrderUser::getCreateTime, LocalDateTime.now()));
        response.setOrderNum(orderUsers.size());
        // 总订单数
        List<OrderUser> orderUsers1 = orderUserMapper.selectList(new QueryWrapper<>());
        response.setTotalOrderNum(orderUsers1.size());
        // 今天交易额 1582
      //  List<BigDecimal> collect = orderUsers.stream().map(OrderUser::getPayAmount).collect(Collectors.toList());
        Integer payAmount = orderUserMapper.getPayAmount(LocalDate.now());
        //总交易额
        Integer payAmountSum = orderUserMapper.getPayAmountSum();
        response.setAmount(BigDecimal.valueOf(payAmount));
        response.setTotalAmount(BigDecimal.valueOf(payAmountSum));
        return response;
    }

    public static void main(String[] args) {
        LocalDate date = LocalDate.now().plusDays(-1);
        LocalDate date1 = LocalDate.now().plusDays(1);
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.now().plusDays(1);
       // LocalDateTime.of(LocalDateTime.now().getDayOfYear(), LocalDateTime.now().getMonth(),LocalDateTime.now().get())
        System.out.println(localDateTime);
        System.out.println(now);
        System.out.println(date);
        System.out.println(date1);
    }

}
